package com.example.mydemo;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import java.util.ArrayList;
import java.util.List;


public class MainActivity2 extends AppCompatActivity{
    private static final int PERMISSION_REQUEST_CODE = 1;


    private Button button,btnTest,btnTestCamer,btnTestVideo;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        button = findViewById(R.id.button);
        btnTest = findViewById(R.id.btnTest);
        btnTestCamer = findViewById(R.id.btnTestCamer);
        btnTestVideo = findViewById(R.id.btnTestVideo);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             startActivity(new Intent(MainActivity2.this,MainActivity.class));
            }
        });
        btnTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                startActivity(new Intent(MainActivity2.this, CameraTestChildActivity.class));
                startActivity(new Intent(MainActivity2.this, ImageTestActivity.class));
            }
        });
        btnTestCamer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity2.this,CameraTestActivity.class));
            }
        });
        btnTestVideo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity2.this,VideoTestActivity.class));
            }
        });
        // 检查并请求权限
        checkAndRequestPermissions();

    }

    private void checkAndRequestPermissions() {
        List<String> permissionsToRequest = new ArrayList<>();

        // 检查文件读写权限
//        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
//                != PackageManager.PERMISSION_GRANTED) {
//            permissionsToRequest.add(Manifest.permission.READ_EXTERNAL_STORAGE);
//        }
//
//            if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
//                    != PackageManager.PERMISSION_GRANTED) {
//                permissionsToRequest.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
//            }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            if (!Environment.isExternalStorageManager() ||ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA)
                    != PackageManager.PERMISSION_GRANTED) {
                Intent intent = new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
                startActivityForResult(intent, 33333);

            }
        }
        // 检查相机权限
//        if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA)
//                != PackageManager.PERMISSION_GRANTED) {
//            permissionsToRequest.add(Manifest.permission.CAMERA);
//        }
//
//
//        // 如果有需要请求的权限，则发起请求
//        if (!permissionsToRequest.isEmpty()) {
//            String[] array = permissionsToRequest.toArray(new String[0]);
//            ActivityCompat.requestPermissions(this,
//                    array,
//                    PERMISSION_REQUEST_CODE);
//        }
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == PERMISSION_REQUEST_CODE) {
            boolean allPermissionsGranted = true;
            for (int result : grantResults) {
                if (result != PackageManager.PERMISSION_GRANTED) {
                    allPermissionsGranted = false;
                    break;
                }
            }
            if (allPermissionsGranted) {
                Log.e("++++++++++", "onRequestPermissionsResult: 所有权限已授予" );
            } else {
                Log.e("++++++++++", "onRequestPermissionsResult: 部分权限未授予" );
            }
        }else if (requestCode == 33333){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                Log.e("++ Mr.Zan ++", "onRequestPermissionsResult: "+Environment.isExternalStorageManager());
                if (Environment.isExternalStorageManager()) {
                    // 用户授予了权限，执行相应操作
                    checkAndRequestPermissions();
                } else {
                    // 用户拒绝了权限，给出提示

                }
            }
        }
    }


}